package com.cgtong.android.mvp.model;

import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.LoginContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;
import com.zhouyou.http.subsciber.IProgressDialog;


/**
 * Created by Sky.Bing on 2017/10/23.
 */
public class LoginModel implements LoginContract.Model {

    @Override
    public void sendValidCode(String phone, SimpleCallBack<Object> callBack) {
        EasyHttp.post(ApiManager.URL_SEND_VALIDCODE)
                .params("mobile", phone)
                .params("codeType", ConstantManager.VALID_CODE_LOGIN)
                .execute(new CallBackProxy<ApiResult<Object>, Object>(callBack) {
                });
    }

    @Override
    public void doLoginByDialog(String phone, String password, String mobileInfo, IProgressDialog dialog, SimpleCallBack<String> callBack) {
        doLoginByDialog(phone, password, mobileInfo, "", dialog, callBack);
    }

    @Override
    public void doLoginByDialog(String phone, String password, String mobileInfo, String validCode, final IProgressDialog dialog, final SimpleCallBack<String> callBack) {
        CallBack loginCallBack = null;
        if (null != dialog) {
            loginCallBack = new ProgressDialogCallBack<String>(dialog, true, true) {
                @Override
                public void onError(ApiException e) {
                    super.onError(e);
                    if (null != callBack)
                        callBack.onError(e);
                }

                @Override
                public void onSuccess(String s) {
                    if (null != callBack)
                        callBack.onSuccess(s);
                }
            };
        } else {
            loginCallBack = new SimpleCallBack<String>() {
                @Override
                public void onError(ApiException e) {
                    if (null != callBack)
                        callBack.onError(e);
                }

                @Override
                public void onSuccess(String s) {
                    if (null != callBack)
                        callBack.onSuccess(s);
                }
            };
        }
        EasyHttp.post(ApiManager.URL_LOGIN).params("phone", phone)
                .params("password", password)
                .params("validcode", validCode)
                .params("mobileInfo", mobileInfo)
                .sign(true)
                .timeStamp(true)
                .execute(loginCallBack);
    }
}